package com.guoq.controller;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.guoq.constant.Configration;
import com.guoq.constant.SessionDatas;
import com.guoq.pojo.Result;
import com.guoq.pojo.Sms;
import com.guoq.pojo.User;
import com.guoq.service.ISmsService;
import com.guoq.service.IUserService;

/**
 * 
 * <P>类名称:IndexController
 * <P>类描述:  默认首页
 * <P>创建人:郭强  
 * <P>修改人:XXX 
 * <P>修改时间:2017年7月18日 下午5:04:21
 * <P>修改备注: 
 * @version 1.0.0
 * Copyright(c)2017西部在线-版权所有
 */
@Controller
public class IndexController {
	
	@Resource
	private ISmsService smsService;
	
	@Resource
	private IUserService userService;
	
	@RequestMapping("")
	public String toIndex(HttpServletRequest request,Model model){
		return "/login";
	}
	
	@RequestMapping("/userLogin")
	public String userLogin(HttpServletRequest request,Model model,User bean) throws Exception{
		
		String phonenumber = request.getParameter("phonenumber");
		String code = request.getParameter("code");
		
		User user = userService.getUserByUsername(phonenumber, Configration.BUSINESS);
		Sms sms = smsService.getByPhonenumber(phonenumber);		//获取验证码信息
		request.getSession().setAttribute(SessionDatas.USER, user);
		
		Result result = new Result();
		
		if(user != null && user.getUsername().equals(phonenumber) && user.getPwd().equals(code)){
			request.getSession().setAttribute("user", user);
			model.addAttribute("result", result);
			return "/index";
		}else{
			result.setStatus(500);
			result.setMsg("验证码不正确");
			return "/login";
		}
		
	}
	
	@RequestMapping("/custLogin")
	public String custLogin(HttpServletRequest request,Model model,User bean) throws Exception{
		
		String phonenumber = request.getParameter("phonenumber");
		String code = request.getParameter("code");
		
		User user = userService.getUserByUsername(phonenumber, Configration.CUSTOMER);
		
		Result result = new Result();
		
		if(user != null && user.getUsername().equals(phonenumber) && user.getPwd().equals(code)){
			request.getSession().setAttribute(SessionDatas.USER, user);
			model.addAttribute("result", result);
			return "/index";
		}else{
			result.setStatus(500);
			result.setMsg("验证码不正确");
			return "/login";
		}
	}
	
//	/**
//	 * 
//	 * @时间：2017年7月14日 下午6:32:41
//	 * @作者：chen.an
//	 */
//	@RequestMapping("login")
//	public String login(@RequestParam Map<String, String> params,Model model) {
//		Captcha captcha  = (Captcha)((ServletRequestAttributes)RequestContextHolder.getRequestAttributes()).getRequest().getSession().getAttribute(SessionDatas.KAPTCHA_KEY);
//		String code = params.get("inputRandomCode");
//		String name = params.get("uname");
//		String pwd = params.get("upass");
//		try {
//			if(StringUtils.isEmpty(name)){
//				model.addAttribute("Msg", "用户名不能为空！");
//			}else if (StringUtils.isEmpty(pwd)) {
//				model.addAttribute("Msg", "密码不能为空！");
//			}else if (!captcha.text().equalsIgnoreCase(code)) {
//				model.addAttribute("Msg", "验证码错误！");
//			} else {
//				pwd = com.guoq.utils.SecurityUtils.commonEncode(pwd, com.guoq.utils.SecurityUtils.KEY_SHA);
//				((ServletRequestAttributes)RequestContextHolder.getRequestAttributes()).getRequest().getSession().removeAttribute(SessionDatas.KAPTCHA_KEY);
//				UsernamePasswordToken token = new UsernamePasswordToken(name,pwd);
//				token.setRememberMe(true);
//				Subject subject = SecurityUtils.getSubject();
//				subject.login(token);
//				System.out.println("登录成功");
//				return "redirect:/home"; 
//			}
//		} catch (AccountException e) {
//			model.addAttribute("Msg", e.getMessage());
//		} catch (UserPasswordException e) {
//			model.addAttribute("Msg", e.getMessage());
//		} catch (UserDisableException e) {
//			model.addAttribute("Msg", e.getMessage());
//		} catch (AuthenticationException e) {
//			model.addAttribute("Msg", "用户名或密码错误");
//		}catch (Exception e) {
//			e.printStackTrace();
//			model.addAttribute("Msg", "登录失败");
//		}
//		return "login";
//	}
	
	/**
	 * 
	 *<P>开发者:郭强
	 *<P>方法名: vcodelogin
	 *<P>描述: 验证码登录
	 *@param request
	 *@param model
	 *@return
	 *@throws Exception   
	 *@exception   
	 *@since  1.0.0
	 */
	@RequestMapping("/vcodelogin")
	@ResponseBody
	public Result vcodelogin(HttpServletRequest request,Model model,User bean) throws Exception {
		
		String phonenumber = request.getParameter("phonenumber");
		String vcode = request.getParameter("code");
		
		User user = userService.getUserByUsername(phonenumber, Configration.CUSTOMER);
		Sms sms = smsService.getByPhonenumber(phonenumber);		//获取验证码信息
		request.getSession().setAttribute(SessionDatas.USER, user);
		
		Result result = new Result();
		
		if(sms != null && sms.getPhonenumber().equals(phonenumber) && sms.getVcode().equals(vcode)){
			result = Result.ok();
		}else{
			request.getSession().invalidate();
			result.setStatus(500);
			result.setMsg("验证码不正确");
		}
		
		return result;
	}
	
	/**
	 * 
	 *<P>开发者:郭强
	 *<P>方法名: pwdlogin
	 *<P>描述: 密码登录
	 *@param request
	 *@param model
	 *@return
	 *@throws Exception   
	 *@exception   
	 *@since  1.0.0
	 */
	@RequestMapping("/pwdlogin")
	@ResponseBody
	public Result pwdlogin(HttpServletRequest request,Model model,User bean) throws Exception {
		
		String phonenumber = request.getParameter("phonenumber");
		String pwd = request.getParameter("pwd");
		
		User user = userService.getUserByUsername(phonenumber, Configration.CUSTOMER);
		Sms sms = smsService.getByPhonenumber(phonenumber);		//获取验证码信息
		request.getSession().setAttribute(SessionDatas.USER, user);
		
		Result result = new Result();
		if(sms != null && user.getUsername().equals(phonenumber) && user.getPwd().equals(pwd)){
			result = Result.ok();
		}else{
			request.getSession().invalidate();
			result.setStatus(500);
			result.setMsg("密码不正确");
		}
		
		return result;
	}
	
	/**
	 * 
	 *<P>开发者:郭强
	 *<P>方法名: register
	 *<P>描述: 注册
	 *@param request
	 *@param model
	 *@return
	 *@throws Exception   
	 *@exception   
	 *@since  1.0.0
	 */
	@RequestMapping("/register")
	@ResponseBody
	public Result register(HttpServletRequest request,Model model,User bean) throws Exception {
		
		String phonenumber = request.getParameter("phonenumber");
		String vcode = request.getParameter("vcode");
		
		Sms sms = smsService.getByPhonenumber(phonenumber);		//获取验证码信息
		
		Result result = new Result();
		if(sms.getPhonenumber().equals(phonenumber) && sms.getVcode().equals(vcode)){
			result = this.userService.create(bean);
		}else{
			result.setStatus(500);
			result.setMsg("验证码不正确");
		}
		
		return result;
	}
	
	/**
	 * 登出
	 * 
	 * @时间：2017年7月14日 下午7:04:55
	 * @作者：chen.an
	 */
//	@ResponseBody
//	@RequestMapping("logout")
//	public void logout() {
//		try {
//			Subject subject = SecurityUtils.getSubject();
//			subject.logout();
//			this.getResponse().sendRedirect("/index.jsp");
//		} catch (IOException e) {
//			e.printStackTrace();
//		}
//	}
	
	/**
	 * 
	 *<P>开发者:张树林
	 *<P>方法名: main
	 *<P>描述:工作台 
	 *@return   
	 *@exception   
	 *@since  1.0.0
	 */
	@RequestMapping("/main")
	public String main(Model model){
		
		Date date = new Date();
		String[] weeks = {"星期日","星期一","星期二","星期三","星期四","星期五","星期六"};
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        int week_index = cal.get(Calendar.DAY_OF_WEEK) - 1;
        SimpleDateFormat format = new SimpleDateFormat("yyyy/MM/dd");
        
        model.addAttribute("time", format.format(date));
        model.addAttribute("week",weeks[week_index]);
		return "main";
	}
}
